package org.jks.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.constant.FileType;
import org.jks.model.FileNode;
import org.jks.response.model.ActionResult;
import org.jks.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/filesystem")
public class FileSystemManagerController {
	@RequestMapping("/")
	public void index(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model){
		ActionResult ar = new ActionResult();
		ar.setSuccess(true);
		String filePath = request.getServletContext().getRealPath("/");
		File root = new File(filePath);
		File[] files = root.listFiles();
		List<FileNode> nodes = new ArrayList<FileNode>();
		for(File file:files){ 
			FileNode fileNode = new FileNode();
			fileNode.setName(file.getName());
			fileNode.setPath(file.getAbsolutePath());
		    if(file.isDirectory()){
		    	fileNode.setType(FileType.DIRECTORY);
		    }
		    else{
		    	fileNode.setType(FileType.FILE);
		    	fileNode.setSize(file.length());
		    }
		    nodes.add(fileNode);
		}
		ar.setData(nodes);
		ResponseUtil.returnJson(response, ar);
	}
}
